/**
*
* author TienNM
* Aug 22, 2014
*
*/

package vn.fsoft.gst.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.fsoft.gst.domain.Organisation;
import vn.fsoft.gst.domain.SupportingMaterial;
import vn.fsoft.gst.domain.User;
import vn.fsoft.gst.service.OrganisationService;
import vn.fsoft.gst.service.SupportingMaterialService;
import vn.fsoft.gst.service.UserService;

@Controller
public class SupportingMaterialController {

	@Autowired
	private SupportingMaterialService suppService;
	@Autowired
	private UserService userService;
	@Autowired
	private SupportingMaterial suppMaterial;
	@Autowired
	private OrganisationService orgService;

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/listSupportingMaterial", method = RequestMethod.GET)
	public String listAll(@RequestParam(value="orgId")int orgID, ModelMap model) {
		model.addAttribute("list", suppService.getByOrg(orgID));
		return "listSupportingMaterial";
	}

	@RequestMapping(value = "/addSupportingMaterial", method = RequestMethod.GET)
	public String loadSuppAdd(@RequestParam(value="orgId") int orgId, ModelMap model) {
		
		List<String> listType = new ArrayList<String>();
		listType.add("doc");
		listType.add("pdf");
		listType.add("excel");
		
		User user = userService.getUserById(1);
		Organisation org = orgService.getOrganisationByID(orgId);
		Date date = new Date();
		SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy");
		
		model.put("listType", listType);
		model.put("user", user);
		model.put("addedDate", fm.format(date));
		model.put("org", org);
		model.addAttribute("suppMaterial", suppMaterial);

		return "addSupportingMaterial";
	}

	@RequestMapping(value = "/addSupportingMaterial", method = RequestMethod.POST)
	public String processadd(@ModelAttribute(value = "suppMaterial") SupportingMaterial suppMaterial) {
		System.out.println(suppMaterial.getOrganisation().getOrgID());
		suppMaterial.setAddedDate(new Date());
		User user = userService.getUserById(1);
		suppMaterial.setUser(user);
		
		suppService.addSupportingMaterial(suppMaterial);
		
		return "redirect:orgDetail?orgId="+suppMaterial.getOrganisation().getOrgID();
	}
	
	@RequestMapping(value="/activeSupportingMaterial", method = RequestMethod.GET)
	public void activeSupportingMaterial(@RequestParam(value = "suppId") int suppId, ModelMap model){
		suppService.activeSupportingMaterial(suppId);
	}
	
	@RequestMapping(value="/editSupportingMaterial", method = RequestMethod.GET)
	public String editSupportingMaterial(@RequestParam("suppId") int Id, ModelMap modelMap){
		SupportingMaterial suppMaterial = suppService.getById(Id);
		List<String> listType = new ArrayList<String>();
		listType.add("doc");
		listType.add("pdf");
		listType.add("excel");
		modelMap.put("listType", listType);
		modelMap.addAttribute("suppMaterial", suppMaterial);
		return "editSupportingMaterial";	
	}
	
	@RequestMapping(value="/editSupportingMaterial", method =  RequestMethod.POST)
	public String processedit(@ModelAttribute(value="suppMaterial") SupportingMaterial suppMaterial,
			ModelMap modelMap){
		suppService.editSupportingMaterial(suppMaterial);
		return "redirect:orgDetail?orgId="+suppMaterial.getOrganisation().getOrgID();
	}

}